package cn.skyclass.utils
{
	/**
	 * 用于路径操作的工具类
	 */
	public class PathUtil
	{
		/**
		 * 获取文件名称的扩展名，仅作字符串分析工作；如 file1.txt --> .txt
		 * return example: .txt
		 */
		public static function getFileExtensionName(fileName:String):String{
			var out:String = fileName;
			if(fileName){
				var i:int = fileName.lastIndexOf(".");
				if(i >= 0){
					out = fileName.substring(i, fileName.length);
				}else{
					out = "";
				}
			}
			return out;			
		}
		
		/**
		 * 获取没有扩展名的文件名；如 file1.txt --> file1
		 */
		public static function getFileNameWithoutExtension(fileName:String):String{
			var out:String = fileName;
			if(fileName){
				var i:int = fileName.lastIndexOf(".");
				if(i >= 0){
					out = fileName.substring(0, i);
				}
			}
			return out;			
		}
		
		/**
		 * 模拟返回全局唯一的文件名，使用当前时间的毫秒数 + 随机小数的形式
		 * 如：118737346307802040557861328125.jpg
		 */
		public static function generateGuidFileName(extensionName:String):String{
			var d:Date = new Date();
			return d.time.toString() + Math.random().toString() + extensionName;
		}		
		
		/**
		 * 匹配扩展名，查看指定的扩展名是否在指定的集合中
		 * 不区分字符大小写
		 */	
		public static function matchExtension(extension:String, extensionsArray:Array):Boolean{
			if(!extension || !extensionsArray){ return false; }			
			
			extension = extension.toLowerCase();
			for each(var s:String in extensionsArray){
				if(s.toLowerCase() == extension){ return true; }
			}
			return false;			
		}				
			
			
		// 几种常见的扩展名集合
		
		public static const picExtensions:Array = 
			[".gif", ".jpg", ".jpeg", ".bmp", ".png", ".ico", ".pic"];
		
		public static const videoExtensions:Array = 
			[".mpg", ".mpeg", ".avi", ".rm", ".rmvb", ".wmv", ".asf", ".dat", ".flv"];
		
		public static const audioExtensions:Array = 
			[".mp3", ".wma", ".wav", ".mid", ".midi"];
		
		public static const flashExtensions:Array = [".swf", ".flv"];
		
		public static const docExtensions:Array = 
			[".txt", ".rtf", ".doc", ".xls", ".ppt", ".chm"];
		
		public static const webExtensions:Array = 
			[".htm", ".html", ".mht", ".asp", ".aspx", ".jsp", ".php"];
			
		public static const plainTextExtensions:Array = [".htm", ".html", ".txt"];			
			
	}
}